package cc.rengu.igas.channel.upac.core.service.trans;


import cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.common.enums.UpacProdEnum;
import cc.rengu.igas.channel.upac.common.util.UpacSecurityUtil;
import cc.rengu.igas.channel.upac.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upac.core.service.base.TokenPayService;
import cc.rengu.igas.channel.upac.facade.bean.CustomerInfo;
import cc.rengu.igas.channel.upac.facade.bean.TokenPayInfo;
import cc.rengu.igas.channel.upac.facade.request.TokenPayOpenQueryRequest;
import cc.rengu.igas.channel.upac.facade.response.TokenPayOpenQueryResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;

import java.util.HashMap;
import java.util.Map;

/**
 * 无跳转支付开通查询
 */
public class TokenPayOpenQueryService extends TokenPayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        TokenPayOpenQueryRequest tokenPayOpenQueryRequest = new TokenPayOpenQueryRequest();
        ConvertUtil.convertOutput(tokenPayOpenQueryRequest);
        return tokenPayOpenQueryRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        ChnlBizResponse<TokenPayOpenQueryResponse> chnlBizResponse = new ChnlBizResponse<>();
        TokenPayOpenQueryResponse tokenPayOpenQueryResponse = new TokenPayOpenQueryResponse();
        TokenPayOpenQueryRequest tokenPayOpenQueryRequest = (TokenPayOpenQueryRequest) request;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        ConvertUtil.convertInput(tokenPayOpenQueryRequest);
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnType, "78");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnSubType, "02");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.bizType, UpacProdEnum.TOKEN_PAY.getProdCode());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, tokenPayOpenQueryRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            tokenPayOpenQueryResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            tokenPayOpenQueryResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(tokenPayOpenQueryResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(tokenPayOpenQueryResponse);

        //TOKEN支付信息
        String tokenPayData = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.tokenPayData);
        if (tokenPayData != null) {
            rglog.debug("tokenPayData:<{}>", tokenPayData);
            TokenPayInfo tokenPayInfo = (TokenPayInfo) convertKvStr2Bean(tokenPayData, TokenPayInfo.class);
            tokenPayOpenQueryResponse.setTokenPayInfo(tokenPayInfo);
        }

        //身份认证信息
        String customerInfoBase64Str = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.customerInfo);
        if (customerInfoBase64Str != null) {
            CustomerInfo customerInfo = new CustomerInfo();
            String customerInfoStr = new String(Base64.decodeBase64(customerInfoBase64Str));
            Map<String, String> customer = UpacSecurityUtil.convertResultString2Map(customerInfoStr);
            rglog.debug("customerInfoStr=[{}]", JSON.toJSONString(customer));
            if (customer.get("customerNm") != null) {
                customerInfo.setCustName(customer.get("customerNm"));
            }
            if (customer.get("certifTp") != null) {
                customerInfo.setCertType(customer.get("certifTp"));
            }
            if (customer.get("certifId") != null) {
                customerInfo.setCertNo(customer.get("certifId"));
            }
            if (customer.get("encryptedInfo") != null) {
                String encStr = descryptData(customer.get("encryptedInfo"));
                Map<String, String> encMap = UpacSecurityUtil.convertResultString2Map(encStr);
                if (encMap.get("phoneNo") != null) {
                    customerInfo.setPhoneNo(encMap.get("phoneNo"));
                }
                if (encMap.get("expired") != null) {
                    customerInfo.setExpiryDate(encMap.get("expired"));
                }
            }
            tokenPayOpenQueryResponse.setCustomerInfo(customerInfo);
        }

        tokenPayOpenQueryResponse.setRespCode(convertRespCode(tokenPayOpenQueryResponse.getRespCode()));
        chnlBizResponse.setResult(tokenPayOpenQueryResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        TokenPayOpenQueryResponse tokenPayOpenQueryResponse = (TokenPayOpenQueryResponse) response.getResult();
        ConvertUtil.convertInput(tokenPayOpenQueryResponse);
    }


}
